# -*- coding: utf-8 -*-
import logging

from odoo import http
from odoo.http import request
from odoo.tools import topological_sort
from odoo.tools.translate import _
_logger = logging.getLogger(__name__)

from odoo import http
from odoo.http import content_disposition, dispatch_rpc, request, \
    serialize_exception as _serialize_exception, Response
import base64 


import werkzeug
import werkzeug.exceptions
import werkzeug.utils

class fhwos_Controller(http.Controller):

    @http.route(['/myattachment/publice_index'], type='http', auth='user')
    def show_publice_property_report_index(self, debug=False, **k):
        #lself= http.request.env[fhmodel].search([(fhparent,"=",False)])
        values={}
        values['error'] = _("Wrong login/password")
        return request.render('myattachment.publice_index', values) 
       

    @http.route(['/myattachment/publice_index_report'], type='http', auth='user')
    def show_publice_property_report(self, debug=False, **k):
        #lself= http.request.env[fhmodel].search([(fhparent,"=",False)])
        values={}
        values['error'] = _("Wrong login/password")
        return request.render('myattachment.publice_index_report', values)  


        
    @http.route(['/fhattachment/done'],type='http', auth="public", methods=['POST'], csrf=False)
    def portal_chatter_post(self, res_model=None, res_id=None, message=None, **kw):
        if kw.get('ufile'):
            attachment_ids = []
            files = request.httprequest.files.getlist('ufile')
            Model = request.env['ir.attachment']
            for ufile in files:
                filename = ufile.filename
                if request.httprequest.user_agent.browser == 'safari':
                    filename = unicodedata.normalize('NFD', ufile.filename)
                #if(1==1):
                try:
                    attachment = Model.sudo().create({
                        'name': filename,
                        'datas': base64.b64encode(ufile.read()),
                        'datas_fname': filename,
                        'res_model': request.session.res_model,
                        'res_id':request.session.res_id
                    })
                    attachment_ids.append(attachment.id)
                    print('attachment.id',attachment.id)
                except Exception:
                    return "上传文件失败!"
        #url=request.session.url 
        #return werkzeug.utils.redirect(url, 302)
        out = """<script language="javascript" type="text/javascript">
                     window.history.go(-2);
                </script>"""        
        return out          
